
//
// 110804a- AH - (Bybanen *) Created.
// 110804b- AH - Moved from package 
//               org.soekrat.gui.swing.chat.objectconversation.note
// 110805a- AH - Renamed from TreeFolderPanel and now extends
//               ConversationComponent.TreeFolderConversationComponent.
// 110805b- AH - Completed.
// 110805c- AH - Ignore artifact root.
// 110805d- AH - Renamed from TreeFolderConversationComponent.
//

package org.soekrat.gui.swing.chat.objectconversation.forest;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.forest.Tree;
import org.soekrat.engine.pit.Pit;
import org.soekrat.gui.swing.TestFrame;
import org.soekrat.gui.swing.chat.objectconversation.ConversationComponent;

/**
 * <p>
 *   TODO
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class ForestFolderConversationComponent 
  extends ConversationComponent 
{
  private final JButton m_up;
  
  private final JButton m_link;
  
  private final JButton m_note;
  
  private final JButton m_down;
  
  private final Tree m_root;
  
  private Tree m_current;
  
  private final JLabel m_name=new JLabel();
  
  private final JPanel m_north=new JPanel(new FlowLayout(FlowLayout.LEFT));
  
  private final JPanel m_buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link Soekrat2Engine} interfaced.
   * @param root   Root in tree to view.
   */
  public ForestFolderConversationComponent(Soekrat2Engine engine, Tree root)
  {
    super(engine, "forest");
    
    if (root==null)
    {
      throw new NullPointerException("root");
    }
    
    Tree asRoot=root;
    if (!root.isLeaf() && root.isRoot() && root.getName().equals("root"))
    {
      asRoot=root.getChild(0);
    }
    
    
    m_current=m_root=asRoot;
    
    setLayout(new BorderLayout());
    
    add(m_buttons, BorderLayout.CENTER);
    
    m_up=new JButton(m_upAction);
    
    m_link=new JButton(m_linkAction);
    
    m_note=new JButton(m_noteAction);
    
    m_down=new JButton(m_downAction);
    
    m_north.add(m_up);
    m_north.add(m_name);
    add(m_north, BorderLayout.NORTH);
    
    setView();
  }
  
  private void setView()
  {
    Pit pit=getSoekratEngine().getPit();
    
    String name=m_current.getName();
    
    String ontology=getOntology();
    
    m_up.setEnabled(m_current!=m_root);
    
    m_name.setText(name);
    
    m_north.add(m_name);
    
    m_buttons.removeAll();
    
    if (pit.hasContent(name, ontology) && !isPureNote(name))
    {
      m_buttons.add(m_link);
    }
    
    m_buttons.add(m_note);
    
    if (!m_current.isLeaf())
    {
      m_buttons.add(m_down);
    }
    
    m_north.repaint();
    m_buttons.repaint();
  }
  
  private final Action m_upAction=new AbstractAction()
  {
    {
      URL url=ClassLoader.getSystemResource(
        "org/soekrat/gui/swing/chat/objectconversation/forest/UpArrow.png");
      if (url!=null)
      {
        Icon icon=new ImageIcon(url);
        putValue(SMALL_ICON, icon);
      }
    }
    
    public void actionPerformed(ActionEvent e)
    {
      m_current=m_current.getParent();
      setView();
    }
  };
  
  private final Action m_linkAction=new AbstractAction()
  {
    {
      URL url=ClassLoader.getSystemResource(
        "org/soekrat/gui/swing/chat/objectconversation/forest/Link.png");
      if (url!=null)
      {
        Icon icon=new ImageIcon(url);
        putValue(SMALL_ICON, icon);
      }
    }
    
    public void actionPerformed(ActionEvent e)
    {
      String name=m_current.getName();
      showContent(name);
    } 
  };
  
  private final Action m_noteAction=new AbstractAction()
  {
    {
      URL url=ClassLoader.getSystemResource(
        "org/soekrat/gui/swing/chat/objectconversation/forest/Note.png");
      if (url!=null)
      {
        Icon icon=new ImageIcon(url);
        putValue(SMALL_ICON, icon);
      }
    }
    
    public void actionPerformed(ActionEvent e){ setNote(m_current.getName()); }
    
  };
  
  private final Action m_downAction=new AbstractAction()
  {
    {
      URL url=ClassLoader.getSystemResource(
        "org/soekrat/gui/swing/chat/objectconversation/forest/Folder.png");
      if (url!=null)
      {
        Icon icon=new ImageIcon(url);
        putValue(SMALL_ICON, icon);
      }
    }
    
    public void actionPerformed(ActionEvent e)
    {
      m_current=m_current.getChild(0);
      setView();
    }
    
  };
  
  public static void main(String[] args) 
  {
    Tree root=new Tree("root");
    Tree child=root.createChild("child");
    Tree childchild=child.createChild("leaf");
    
    Soekrat2Engine engine=new Soekrat2Engine();
    TestFrame.show(new ForestFolderConversationComponent(engine, root));  
  }
  
}
